package cn.gok.service.impl;

import cn.gok.dao.NewStudentDao;
import cn.gok.dao.StudentDao;
import cn.gok.entity.Course;
import cn.gok.entity.Student;
import cn.gok.entity.Teacher;
import cn.gok.service.NewStudentService;
import cn.gok.util.R;
import cn.gok.util.Sqlsessionutil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NewStudentServiceImpl implements NewStudentService {
    private SqlSession sqlSession;
    private NewStudentDao newStudentDao;

    public NewStudentServiceImpl(){
        sqlSession = Sqlsessionutil.getSqlSessionFactory().openSession(true);
        newStudentDao = sqlSession.getMapper(NewStudentDao.class);
    }
    @Override
    public R login(String name, String password) {
        Student login = newStudentDao.login(name, password);
        if (login!=null){
            R success = R.success();
            success.put("student",login);
            return success;
        }
        else {
            Teacher teacher = newStudentDao.login1(name, password);
            if (teacher!=null){
                R r = R.success1();
                r.put("teacher",teacher);
                return r;
            }
        }
        return R.fail();
    }

    @Override
    public R checkPassword(String password, int stuId) {
        String s = newStudentDao.checkPassword(password, stuId);
        if (s!=null){
            return R.success();
        }
        return R.fail();
    }

    @Override
    public R changePassword(String password, int stuId) {
        int i = newStudentDao.changePassword(password, stuId);
        if (i>0){
            return R.success();
        }
        return R.fail();
    }

    @Override
    public R selectCourse(String couName, String place, String state,int currentPage, int pageSize) {
        List<Course> courses = newStudentDao.selectCourse(couName, place, state);
        PageHelper.startPage(currentPage,pageSize);

        PageInfo pageInfo=new PageInfo(courses);
        R r=new R<>();
        r.put("result",true);
        r.put("msg","");

        Map<String ,Object> pagination=new HashMap<>();
        pagination.put("currentPage",currentPage);
        pagination.put("pageSize",pageSize);
        pagination.put("total",pageInfo.getTotal());

        r.put("pagination",pagination);
        r.put("datas",courses);

        return r;
    }

    @Override
    public R selectCourseById(String couId) {
        Course course = newStudentDao.selectCourseById(couId);
        if (course!=null){
            R success = R.success();
           success.put("course", course);
           return success;
        }
        return R.fail();
    }

    @Override
    public R chooseCourse(String couId, String stuId, String state,String max,String num) {
        if (state.equals("已结束")){
            return R.fail();
        }
        if (max.equals(num)){
            return R.fail();
        }

        int i = newStudentDao.chooseCourse(couId, stuId, state);
        int i1 = newStudentDao.updateCourse(couId);
        if (i>0&&i1>0){
            return R.success();
        }
        return R.fail();
    }

    @Override
    public R selectMyCourse(String stuId,int currentPage, int pageSize) {
        List<Course> courses = newStudentDao.selectMyCourse(stuId);
        PageHelper.startPage(currentPage,pageSize);

        PageInfo pageInfo=new PageInfo(courses);
        R r=new R<>();
        r.put("result",true);
        r.put("msg","");

        Map<String ,Object> pagination=new HashMap<>();
        pagination.put("currentPage",currentPage);
        pagination.put("pageSize",pageSize);
        pagination.put("total",pageInfo.getTotal());

        r.put("pagination",pagination);
        r.put("datas",courses);

        return r;

    }

    @Override
    public R deleteCourse(String couId, String stuId) {
        int i = newStudentDao.deleteCourse(couId, stuId);
        int i1 = newStudentDao.updateCourseD(couId);
        if (i>0&&i1>0){
            return R.success();
        }
        return R.fail();
    }


}
